package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.service.entity.ProductNameEntity;
import io.renren.modules.service.service.ProductNameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author chenzh
 * @email ${email}
 * @date 2019-05-06 23:38:00
 */
@RestController
@RequestMapping("sys/productName")
public class SysProductNameController {
    @Autowired
    private ProductNameService productNameService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:productName:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = productNameService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:productName:info")
    public R info(@PathVariable("id") Integer id) {
        ProductNameEntity productName = productNameService.getById(id);

        return R.ok().put("productName", productName);
    }

    @RequestMapping("/select")
    @RequiresPermissions("sys:productName:select")
    public R select() {
        List<ProductNameEntity> list = productNameService.list();
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @SysLog("保存产品名称")
    @RequestMapping("/save")
    @RequiresPermissions("sys:productName:save")
    public R save(@RequestBody ProductNameEntity productName) {
        ValidatorUtils.validateEntity(productName, AddGroup.class);
        productName.setCreateTime(new Date());
        productNameService.save(productName);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改产品名称")
    @RequestMapping("/update")
    @RequiresPermissions("sys:productName:update")
    public R update(@RequestBody ProductNameEntity productName) {
        ValidatorUtils.validateEntity(productName, UpdateGroup.class);
        productNameService.updateById(productName);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除产品名称")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:productName:delete")
    public R delete(@RequestBody Integer[] ids) {
        productNameService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
